﻿using AutoMapper;
using SH.Common.CommonReturn;
using SH.Room.Application.IServices.IRoomPrice;
using SH.Room.Application.IServices.IRoomType;
using SH.Room.Domain.RoomClass;
using SH.Room.Infrastructure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SH.Room.Application.Services
{
    public class RoomPriceServices : BaseServices<RoomPrice>, IRoomPriceServices
    {
        private readonly IRoomPriceRepository _roomPriceRepository;

        private readonly IRoomTypeRepository _roomTypeRepository;

        private readonly IMapper _mapper;
        public RoomPriceServices(IRoomPriceRepository roomPriceRepository, IMapper mapper, IRoomTypeRepository roomTypeRepository) : base(roomPriceRepository)
        {
            _roomPriceRepository = roomPriceRepository;
            _mapper = mapper;
            _roomTypeRepository = roomTypeRepository;
        }

        /// <summary>
        /// 房价添加
        /// </summary>
        /// <param name="roomPriceAddDto">房价DTO</param>
        /// <returns>受影响行数</returns>
        public async Task<ApiResult<int>> RoomPriceAdd(RoomPriceAddDto roomPriceAddDto)
        {
            ApiResult<int> apiResult= new ApiResult<int>();
            var item = _roomPriceRepository.GetListByCondition(x => x.ProductName == roomPriceAddDto.ProductName).FirstOrDefault();
            if (item == null)
            {
                var roomprice = _mapper.Map<RoomPrice>(roomPriceAddDto);
                await _roomPriceRepository.InsertAsync(roomprice);
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "产品名称已有，请重新添加");
            }
            return apiResult;
        }

        /// <summary>
        /// 房价显示、查询
        /// </summary>
        /// <param name="rtid">房型ID</param>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<RoomPriceListDto>> GetRoomPriceList(int rtid=-1)
        {
            ApiPaging<RoomPriceListDto> apiPaging= new ApiPaging<RoomPriceListDto>();
            var type = _roomTypeRepository.All;
            var price = _roomPriceRepository.All;
            var list = price.LeftJoin<RoomType>((a, b) => a.Rtid == b.Rtid).Where((a, b) =>rtid==-1 || a.Rtid==rtid).OrderByDescending(a=>a.Sort).Select((a, b) => new RoomPriceListDto
            {
                RpId = a.RpId,
                Rtid = a.Rtid,
                Rtname = b.Rtname,
                ProductName = a.ProductName,
                PayMent = a.PayMent,
                NormalPrice = a.NormalPrice,
                Sort = a.Sort,
                State = a.State,
            }).ToList();
            return new ApiPaging<List<RoomPriceListDto>> { Data=list};
        }

        /// <summary>
        /// 修改房价状态
        /// </summary>
        /// <param name="rpid">房价ID</param>
        /// <param name="state">状态</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> GetRoomPriceState(int rpid, bool state)
        {
            ApiResult<int> apiResult= new ApiResult<int>();
            var item = _roomPriceRepository.GetModelByCondition(x => x.RpId == rpid);
            if (item.State == false)
            {
                item.State = true;
            }
            else
            {
                item.State=false;
            }
            var info = _roomPriceRepository.Upd(item);
            apiResult.Success();
            return apiResult;

        }

        /// <summary>
        /// 房价删除
        /// </summary>
        /// <param name="rpid">房价ID</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> DelRoomprice(int rpid)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var item = _roomPriceRepository.DelRoomprice(rpid);
            apiResult.Success();
            return apiResult;
        }

        /// <summary>
        /// 房价反填
        /// </summary>
        /// <param name="rpid">房价ID</param>
        /// <returns>受影响行数</returns>
        public RoomPrice FindRoomPrice(int rpid)
        {
            var item = _roomPriceRepository.GetModelByCondition(x=>x.RpId==rpid);
            return item;
        }

        /// <summary>
        /// 房价修改
        /// </summary>
        /// <param name="roomPriceUpdDto">房价修改DTO</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> UpdRoomPrice(RoomPriceUpdDto roomPriceUpdDto)
        {
            ApiResult<int> apiResult=new ApiResult<int>();
            var item = _mapper.Map<RoomPrice>(roomPriceUpdDto);
            var info = _roomPriceRepository.Upd(item);
            apiResult.Success(info);
            return apiResult;
        }
    }
}
